<template>
    <div class="container">
      <!-- Main Container -->
      <div class="main-card">
        <!-- Decorative Elements -->
        <div class="decor decor-circle-1"></div>
        <div class="decor decor-circle-2"></div>
        <div class="decor decor-diamond"></div>
        <div class="decor decor-star-1">★</div>
        <div class="decor decor-star-2">★</div>
        
        <!-- Title Section -->
        <div class="title-container">
          <div class="title-box">
            <h1 class="title">file外卖柜</h1>
            <div class="title-icon">
              <div class="title-icon-outer">
                <div class="title-icon-inner">
                  <span>-ᴗ-</span>
                </div>
              </div>
            </div>
            <div class="title-dot"></div>
          </div>
        </div>
        
        <!-- Two Columns Layout -->
        <div class="columns-tabs">
         <div class="column-tab" :class="{'active': activeTab === 'get'}" @click="activeTab = 'get'">
           <div class="tab-icon">📥</div>
           <div class="tab-text">GO取</div>
         </div>
         <div class="column-tab" :class="{'active': activeTab === 'store'}" @click="activeTab = 'store'">
           <div class="tab-icon">📤</div>
           <div class="tab-text">GO存</div>
         </div>
       </div>
        <div class="columns">
          <!-- Left Column - Get Files -->
          <div class="column column-left" v-if="activeTab === 'get'">
            <!-- Header -->
            <div class="header-container">
              <div class="header header-get">取</div>
            </div>
            
            <!-- Lock Icon and Input Field with Get Button -->
            <div class="section">
              <div class="lock-section">
                <div class="lock-icon">
                  <div class="lock-body">
                    <div class="lock-handle"></div>
                  </div>
                </div>
                <div class="lock-text">输入开柜码获取文件</div>
              </div>
              <div class="cabinet-code">
              <div class="cabinet-code-label">开柜码</div>
              <div class="input-group">
                <input 
                  type="text" 
                  class="input"
                  v-model="retrieveCode"
                  placeholder="请输入开柜码"
                />
                <button 
                  @click="getFile" 
                  class="button button-get"
                >
                  获取
                </button>
              </div>
              <div v-show="downloadError.length>0" class="error-message">{{ downloadError }}</div>
            </div>
           
            </div>
            
            <!-- Retrieved File Name Display -->
            <div v-if="retrievedFileName" class="file-display">
              <div class="file-display-icon">📄</div>
              <div class="file-display-label">获取到的文件:</div>
              <div class="file-display-content">
                <div class="file-icon">📁</div>
                <div class="file-name">{{ retrievedFileName }}</div>
              </div>
            </div>
            <div v-else class="files-illustration">
              <div class="file file-1"></div>
              <div class="file file-2"></div>
              <div class="file file-3">
                <div class="file-face">ʘ‿ʘ</div>
              </div>
            </div>
            <!-- Download Button -->
            <div class="button-container">
              <button 
                @click="executeDownload" 
                :disabled="!retrievedFileName"
                class="button button-download"
                :class="{ 'button-disabled': !retrievedFileName }"
              >
                下载
                <span class="button-gem">♦</span>
              </button>
            </div>
          </div>
          
          <!-- Right Column - Store Files -->
          <div class="column column-right" v-if="activeTab === 'store'">
            <!-- Header -->
            <div class="header-container">
              <div class="header header-store">存</div>
            </div>
            
            <!-- Lock Icon and Description -->
            <div class="lock-section">
              <div class="lock-icon">
                <div class="lock-body">
                  <div class="lock-handle"></div>
                </div>
              </div>
              <div class="lock-text">允许直入开柜码</div>
            </div>
            
            <!-- Cabinet Code Input (Moved to middle) -->
            <div class="cabinet-code">
              <div class="cabinet-code-label">开柜码</div>
              <input 
                type="text" 
                class="input cabinet-code-input"
                v-model="fileCode"
                placeholder="开柜码在上传文件后显示"
                disabled
              />
            </div>
            
            <!-- File Illustration -->
            <div class="files-illustration">
              <div class="file file-1"></div>
              <div class="file file-2"></div>
              <div class="file file-3">
                <div class="file-face">ʘ‿ʘ</div>
              </div>
            </div>
            
            <!-- Upload Button -->
            <div class="button-container">
              <button class="button button-upload">
                <span class="button-gem button-gem-left">♦</span>
                <input 
                  type="file" 
                  class="file-input" 
                  @change="handleFileUpload"
                />
                上传文件
              </button>
            </div>
          </div>
        </div>
      </div>
      <Toast  ref="toastRef" />
    </div>
  </template>
  
  <script setup>
  import { ref } from 'vue';
  const {proxy} = getCurrentInstance();
  const retrieveCode = ref('');
  const retrievedFileName = ref('');
  const selectedFile = ref(null);
  const activeTab = ref('get');
  const downloadProgress = ref(0);
  const downloadError = ref('');
  console.log(proxy)
  const getFile = async () => {
    if (!/^[A-Z0-9]{6}$/.test(retrieveCode.value)) {
      downloadError.value = '取件码格式不正确（6位字母数字）';

      return;
    }

    try {
      // 第一步：验证取件码并获取元数据
      const meta = await $fetch('/api/download?validate=true', {
        method: 'POST',
        body: { code: retrieveCode.value }
      });

      // 存储文件元数据
      retrievedFileName.value = meta.fileName;
      downloadProgress.value = 0;
      downloadError.value = '';
    } catch (error) {
      console.error('验证失败:', error);
      downloadError.value = error.data?.message || '取件码验证失败';
      retrievedFileName.value = '';
    }
  };

  // 新增下载执行函数
  const executeDownload = async () => {
    try {
      const response = await $fetch('/api/download', {
        method: 'POST',
        body: { code: retrieveCode.value },
        onDownloadProgress: (progressEvent) => {
          downloadProgress.value = Math.round(
            (progressEvent.loaded * 100) / progressEvent.total
          );
        }
      });

      // 创建Blob实现下载
      const blob = new Blob([response]);
      const url = window.URL.createObjectURL(blob);
      const a = document.createElement('a');
      a.href = url;
      a.download = retrievedFileName.value;
      document.body.appendChild(a);
      a.click();
      window.URL.revokeObjectURL(url);
      document.body.removeChild(a);
    } catch (error) {
      console.error('下载失败:', error);
      downloadError.value = error.data?.message || '文件下载失败';
    }
  };
  
  const uploadProgress = ref(0);
const uploadError = ref('');
const fileCode = ref('');

const handleFileUpload = async (event) => {
  const file = event.target.files[0];
  if (!file) return;

  // 文件类型校验
  const allowedTypes = ['application/pdf', 'image/png', 'text/plain'];
  if (!allowedTypes.includes(file.type)) {
    uploadError.value = '仅支持PDF、PNG和TXT文件';
    return;
  }

  selectedFile.value = file;
  uploadError.value = '';
  uploadProgress.value = 0;

  const formData = new FormData();
  formData.append('file', file);

  try {
    const response = await $fetch('/api/upload', {
      method: 'POST',
      body: formData,
      onUploadProgress: (progressEvent) => {
        uploadProgress.value = Math.round(
          (progressEvent.loaded * 100) / progressEvent.total
        );
      }
    });

    fileCode.value = response.code;
    proxy.$refs.toastRef.message('上传成功！');
    console.log('上传成功，取件码:', response.code);
  } catch (error) {
    console.error('上传失败:', error);
    uploadError.value = error.data?.message || '文件上传失败';
  }
};
  </script>
  
  <style scoped>
  /* Base Styles */
  * {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
  }
  
  .container {
    min-height: 100vh;
    background-color: #d8b4fe;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    font-family: Arial, sans-serif;
  }
  
  .main-card {
    width: 100%;
    max-width: 48rem;
    background-color: white;
    border-radius: 1.5rem;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
    padding: 1.5rem;
    position: relative;
    overflow: hidden;
  }
  
  /* Decorative Elements */
  .decor {
    position: absolute;
  }
  
  .decor-circle-1 {
    top: 1rem;
    left: 1rem;
    width: 3rem;
    height: 3rem;
    background-color: #fef08a;
    border-radius: 50%;
  }
  
  .decor-circle-2 {
    top: 1rem;
    right: 1rem;
    width: 3rem;
    height: 3rem;
    background-color: #fef08a;
    border-radius: 50%;
  }
  
  .decor-diamond {
    bottom: 5rem;
    right: 2rem;
    width: 1.5rem;
    height: 1.5rem;
    background-color: #fdba74;
    transform: rotate(45deg);
  }
  
  .decor-star-1 {
    top: 6rem;
    left: 4rem;
    font-size: 2rem;
    color: #facc15;
  }
  
  .decor-star-2 {
    bottom: 10rem;
    right: 3rem;
    font-size: 1.5rem;
    color: #facc15;
  }
  
  /* Title Section */
  .title-container {
    display: flex;
    justify-content: center;
    margin-bottom: 2rem;
    margin-top: 1rem;
  }
  
  .title-box {
    background-color: #c084fc;
    padding: 1rem 3rem;
    border-radius: 9999px;
    position: relative;
  }
  
  .title {
    font-size: 2.25rem;
    font-weight: bold;
    color: #fef08a;
    letter-spacing: 0.1em;
    text-shadow: 3px 3px 0 #3730a3, -1px -1px 0 #3730a3, 1px -1px 0 #3730a3, -1px 1px 0 #3730a3, 1px 1px 0 #3730a3;
  }
  
  .title-icon {
    position: absolute;
    top: -1.5rem;
    left: 1.5rem;
  }
  
  .title-icon-outer {
    width: 2.5rem;
    height: 2.5rem;
    background-color: #bfdbfe;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  
  .title-icon-inner {
    width: 2rem;
    height: 2rem;
    background-color: #93c5fd;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #2563eb;
  }
  
  .title-dot {
    position: absolute;
    top: 0.25rem;
    right: 1rem;
    width: 1rem;
    height: 1rem;
    background-color: #fde047;
    border-radius: 50%;
  }
  
  /* Columns Layout */
  .columns {
    display: grid;
    /* grid-template-columns: 1fr; */
    gap: 1.5rem;
    height: 400px;
  }
  
  @media (min-width: 768px) {
    .columns {
      /* grid-template-columns: 1fr 1fr; */
    }
  }
  .columns-tabs {
    display: flex;
    justify-content: center;
    margin-bottom: 1rem;
  }

  .column-tab{
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0.5rem 1rem;
    border-radius: 0.5rem;
    background-color: #f9fafb;
    margin: 0 0.5rem;
    cursor: pointer;
  }

  .column-tab.active {
    background-color: #bfdbfe;
  }

  .column-tab:hover {
    background-color: #e5e7eb;
  }

  .column-tab .icon {
    margin-right: 0.5rem;
  }

  .column-tab .icon svg {
  }
  .column {
    border-radius: 1.5rem;
    padding: 1rem;
    position: relative;
    width: 100%;
  }
  
  .column-left {
    background: linear-gradient(to right, #dcfce7, #dbeafe);
  }
  
  .column-right {
    background: linear-gradient(to right, #fef9c3, #ffedd5);
  }
  
  /* Headers */
  .header-container {
    display: flex;
    justify-content: center;
    margin-bottom: 1rem;
  }
  
  .header {
    padding: 0.25rem 1.5rem;
    border-radius: 0.5rem;
    font-size: 1.25rem;
    font-weight: bold;
  }
  
  .header-get {
    background-color: #93c5fd;
  }
  
  .header-store {
    background-color: #fde047;
  }
  
  /* Lock Section */
  .lock-section {
    display: flex;
    align-items: center;
    margin-bottom: 1rem;
  }
  
  .lock-icon {
    width: 3rem;
    height: 3rem;
    background-color: #d8b4fe;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 0.75rem;
  }
  
  .lock-body {
    width: 2rem;
    height: 2rem;
    background-color: #c084fc;
    border-radius: 0.25rem;
    position: relative;
  }
  
  .lock-handle {
    width: 1rem;
    height: 1rem;
    border: 2px solid #fde047;
    border-radius: 50%;
    position: absolute;
    top: -0.75rem;
    left: 0.5rem;
  }
  
  .lock-text {
    font-size: 0.875rem;
  }
  
  /* Input Field and Button */
  .section {
    margin-bottom: 1rem;
  }
  
  .input-group {
    display: flex;
    gap: 0.5rem;
  }
  
  .input {
    flex: 1;
    padding: 0.75rem 1rem;
    border-radius: 0.5rem;
    border: 1px solid #e5e7eb;
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
  }
  
  .input:focus {
    border-color: #c084fc;
    box-shadow: 0 0 0 3px rgba(192, 132, 252, 0.2);
  }
  
  .button {
    font-weight: bold;
    border: none;
    cursor: pointer;
    transition: background-color 0.2s;
    position: relative;
  }
  
  .button-get {
    background-color: #60a5fa;
    color: white;
    padding: 0.5rem 1rem;
    border-radius: 0.5rem;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  }
  
  .button-get:hover {
    background-color: #3b82f6;
  }
  
  /* File Display */
  .file-display {
    margin-bottom: 1rem;
    padding: 0.75rem;
    background-color: rgba(255, 255, 255, 0.7);
    backdrop-filter: blur(4px);
    border-radius: 0.75rem;
    border: 2px solid #bfdbfe;
    position: relative;
  }
  
  .file-display-icon {
    position: absolute;
    top: -0.5rem;
    left: -0.5rem;
    width: 1.5rem;
    height: 1.5rem;
    background-color: #fde047;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.75rem;
  }
  
  .file-display-label {
    font-size: 0.875rem;
    font-weight: bold;
    margin-bottom: 0.25rem;
    color: #7e22ce;
  }
  
  .file-display-content {
    display: flex;
    align-items: center;
  }
  
  .file-icon {
    width: 2rem;
    height: 2rem;
    background-color: #dbeafe;
    border-radius: 0.25rem;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 0.5rem;
    color: #3b82f6;
  }
  
  .file-name {
    color: #1e40af;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  
  /* Download Button */
  .button-container {
    display: flex;
    justify-content: center;
  }
  
  .button-download, .button-upload {
    background-color: #fde047;
    color: black;
    padding: 0.75rem 3rem;
    border-radius: 9999px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  }
  
  .button-download:hover, .button-upload:hover {
    background-color: #facc15;
  }
  
  .button-disabled {
    opacity: 0.7;
    cursor: not-allowed;
  }
  
  .button-gem {
    position: absolute;
    right: 0.75rem;
    bottom: 0.25rem;
    color: #ca8a04;
  }
  
  .button-gem-left {
    left: 0.75rem;
    color: #a855f7;
  }
  
  /* Cabinet Code Input */
  .cabinet-code {
    margin-bottom: 1rem;
    background-color: rgba(255, 255, 255, 0.5);
    backdrop-filter: blur(4px);
    padding: 0.75rem;
    border-radius: 0.75rem;
    border: 1px solid #fef08a;
  }
  
  .cabinet-code-label {
    font-size: 0.875rem;
    font-weight: bold;
    margin-bottom: 0.25rem;
    color: #7e22ce;
  }
  
  .cabinet-code-input {
    width: 100%;
    background-color: rgba(255, 255, 255, 0.8);
    border-color: #fef08a;
  }
  
  /* Files Illustration */
  .files-illustration {
    display: flex;
    justify-content: center;
    margin-bottom: 1rem;
    position: relative;
    height: 6rem;
    width: 8rem;
    margin-left: auto;
    margin-right: auto;
  }
  
  .file {
    position: absolute;
    width: 6rem;
    height: 4rem;
    border-radius: 0.25rem;
  }
  
  .file-1 {
    top: 0;
    left: 0;
    background-color: #bfdbfe;
    transform: rotate(6deg);
    z-index: 10;
  }
  
  .file-2 {
    top: 0.5rem;
    left: 1rem;
    background-color: #fde047;
    transform: rotate(-3deg);
    z-index: 20;
  }
  
  .file-3 {
    top: 1rem;
    left: 0.5rem;
    background-color: #e9d5ff;
    z-index: 30;
    display: flex;
    align-items: flex-end;
    justify-content: center;
    padding-bottom: 0.25rem;
  }
  
  .file-face {
    color: #a855f7;
    font-size: 0.75rem;
  }
  
  /* File Upload */
  .file-input {
    position: absolute;
    inset: 0;
    opacity: 0;
    cursor: pointer;
    width: 100%;
    height: 100%;
  }
  .error-message {
    color: red;
font-size: 12px;
  }
  </style>

